package com.StarStudio.Service.Impl;

import cn.hutool.core.util.IdUtil;
import com.StarStudio.DAO.GoodsDAO;
import com.StarStudio.DAO.GoodsEvaluationDAO;
import com.StarStudio.DAO.UserInfoDAO;
import com.StarStudio.Service.GoodsEvaluationService;
import com.StarStudio.Service.RemoteSysUserService;
import com.ruoyi.common.core.Entity.Goods;
import com.ruoyi.common.core.Entity.GoodsEvaluation;
import com.ruoyi.common.core.Entity.UserInfo;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class GoodsEvaluationServiceImpl implements GoodsEvaluationService {

    @Autowired
    GoodsEvaluationDAO goodsEvaluationDAO;

    @Autowired
    UserInfoDAO userInfoDAO;

    @Autowired
    GoodsDAO goodsDAO;

    @Autowired
    RemoteSysUserService remoteSysUserService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertGoodsEvaluation(GoodsEvaluation goodsEvaluation) {
        goodsEvaluation.setId(IdUtil.getSnowflakeNextId());
        goodsEvaluation.setCreateTime(new Date());
        goodsEvaluation.setDelFlag(false);
        goodsEvaluation.setEvaluationPicture(goodsEvaluation.getEvaluationPicture().toString());
        goodsEvaluationDAO.insertGoodsEvaluation(goodsEvaluation);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateGoodsEvaluation(GoodsEvaluation goodsEvaluation) {
        goodsEvaluation.setUpdateTime(new Date());
        if (goodsEvaluation.getEvaluationPicture() != null){
            goodsEvaluation.setEvaluationPicture(goodsEvaluation.getEvaluationPicture().toString());
        }
        return goodsEvaluationDAO.updateGoodsEvaluation(goodsEvaluation);
    }

    @Override
    public List<GoodsEvaluation> selectGoodsEvaluation(GoodsEvaluation goodsEvaluation) {
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        List<GoodsEvaluation> goodsEvaluations;
        if (sysUser.isAdmin()){
            goodsEvaluations = goodsEvaluationDAO.selectGoodsEvaluation(goodsEvaluation);
        }else {
            goodsEvaluations = goodsEvaluationDAO.selectStoreGoodsEvaluation(goodsEvaluation,sysUser.getUserId());
        }
        if (goodsEvaluations!= null && goodsEvaluations.size() > 0){
            for (GoodsEvaluation evaluation : goodsEvaluations) {
                //格式转换
                String s = evaluation.getEvaluationPicture().toString().replace("[", "").replace("]", "").replace(" ", "");
                List<String> stringList = new ArrayList<>();
                if (StringUtils.isBlank(s)) {
                    stringList = Collections.emptyList();
                } else if (s.contains(",")) {
                    stringList = Arrays.asList(s.split(","));
                } else {
                    stringList.add(s);
                }
                evaluation.setEvaluationPicture(stringList);
                //获取评价用户信息
                UserInfo info = userInfoDAO.findByUser_id(evaluation.getUserId());
                Map<String,Object> param = new HashMap<>();
                param.put("userName",info.getReal_name());
                param.put("avatar",info.getAvatar());
                Goods goods = goodsDAO.findByGoods_id(evaluation.getGoodsId());
                param.put("goodsName",goods.getGoods_name());
                SysUser sysUserById = remoteSysUserService.getSysUserById(evaluation.getStoreId());
                param.put("storeName",sysUserById.getNickName());
                evaluation.setParams(param);
            }
        }
        return goodsEvaluations;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteGoodsEvaluation(Long id) {
        goodsEvaluationDAO.deleteGoodsEvaluation(id);
    }

    @Override
    public List<GoodsEvaluation> selectStoreRecentlyEvaluation(Long storeId) {
        List<GoodsEvaluation> goodsEvaluations = goodsEvaluationDAO.selectStoreRecentlyEvaluation(storeId);
        if (goodsEvaluations != null && goodsEvaluations.size() > 0){
            for (GoodsEvaluation goodsEvaluation : goodsEvaluations) {
                //获取评价用户信息
                UserInfo info = userInfoDAO.findByUser_id(goodsEvaluation.getUserId());
                Map<String,Object> param = new HashMap<>();
                param.put("userName",info.getReal_name());
                param.put("avatar",info.getAvatar());
                Goods goods = goodsDAO.findByGoods_id(goodsEvaluation.getGoodsId());
                param.put("goodsName",goods.getGoods_name());
                goodsEvaluation.setParams(param);
                //处理返回的图片URL数组
                String s = goodsEvaluation.getEvaluationPicture().toString().replace("[", "").replace("]", "").replace(" ", "");
                List<String> stringList = new ArrayList<>();
                if (StringUtils.isBlank(s)) {
                    stringList = Collections.emptyList();
                } else if (s.contains(",")) {
                    stringList = Arrays.asList(s.split(","));
                } else {
                    stringList.add(s);
                }
                goodsEvaluation.setEvaluationPicture(stringList);
            }
        }
        return goodsEvaluations;
    }

    @Override
    public List<GoodsEvaluation> selectRecentlyEvaluation() {
        List<GoodsEvaluation> goodsEvaluations = goodsEvaluationDAO.selectRecentlyEvaluation();
        if (goodsEvaluations != null && goodsEvaluations.size() > 0){
            for (GoodsEvaluation goodsEvaluation : goodsEvaluations) {
                //获取评价用户信息
                UserInfo info = userInfoDAO.findByUser_id(goodsEvaluation.getUserId());
                Map<String,Object> param = new HashMap<>();
                param.put("userName",info.getReal_name());
                param.put("avatar",info.getAvatar());
                Goods goods = goodsDAO.findByGoods_id(goodsEvaluation.getGoodsId());
                param.put("goodsName",goods.getGoods_name());
                SysUser sysUserById = remoteSysUserService.getSysUserById(goodsEvaluation.getStoreId());
                param.put("storeName",sysUserById.getNickName());
                goodsEvaluation.setParams(param);
                //处理返回的图片URL数组
                String s = goodsEvaluation.getEvaluationPicture().toString().replace("[", "").replace("]", "").replace(" ", "");
                List<String> stringList = new ArrayList<>();
                if (StringUtils.isBlank(s)) {
                    stringList = Collections.emptyList();
                } else if (s.contains(",")) {
                    stringList = Arrays.asList(s.split(","));
                } else {
                    stringList.add(s);
                }
                goodsEvaluation.setEvaluationPicture(stringList);
            }
        }
        return goodsEvaluations;
    }

    @Override
    public long findAllRecordNumber() {
        return goodsEvaluationDAO.findAllRecordNumber();
    }

    @Override
    public long findStoreAllRecordNumber(Long storeId) {
        return goodsEvaluationDAO.findStoreAllRecordNumber(storeId);
    }

    @Override
    public List<GoodsEvaluation> getGoodsEvaluationByGoodsId(Long goodsId) {
        List<GoodsEvaluation> goodsEvaluationByGoodsId = goodsEvaluationDAO.getGoodsEvaluationByGoodsId(goodsId);
        if (goodsEvaluationByGoodsId != null && goodsEvaluationByGoodsId.size() > 0){
            for (GoodsEvaluation goodsEvaluation : goodsEvaluationByGoodsId) {
                //处理返回的图片URL数组
                String s = goodsEvaluation.getEvaluationPicture().toString().replace("[", "").replace("]", "").replace(" ", "");
                List<String> stringList = new ArrayList<>();
                if (StringUtils.isBlank(s)) {
                    stringList = Collections.emptyList();
                } else if (s.contains(",")) {
                    stringList = Arrays.asList(s.split(","));
                } else {
                    stringList.add(s);
                }
                goodsEvaluation.setEvaluationPicture(stringList);
                //返回用户名和头像
                Map<String,Object> param = new HashMap<>();
                UserInfo byUserId = userInfoDAO.findByUser_id(goodsEvaluation.getUserId());
                param.put("userName",byUserId.getReal_name());
                param.put("userAvatar",byUserId.getAvatar());
                goodsEvaluation.setParams(param);
            }
        }
        return goodsEvaluationByGoodsId;
    }
}
